﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;

namespace LAB3_Cv
{
    public partial class Login : System.Web.UI.Page
    {
        public bool ApplicantLoggedIn { get; set; }
        public bool AdminLoggedIn { get; set; }

        public Login()
        {
            ApplicantLoggedIn = false;
            AdminLoggedIn = false;

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            LabelSocialSecurityPassword.Visible = false;
            //TextBoxSocialSecurity.Text = "8702274054";
            //TextBoxPassword.Text = "123";
        }

        protected void LoginButton_Click(object sender, EventArgs e)
        {
            bool isSocialSecurity = IsSocialSecurityNumber(TextBoxSocialSecurity.Text);

            if (isSocialSecurity == true)
            {

                ApplicantLoggedIn = ApplicantDB.ApplicantIsLoggedIn(TextBoxSocialSecurity.Text, TextBoxPassword.Text);
                if (ApplicantLoggedIn)
                {
                    var currentApplicant = ApplicantDB.GetApplicant(TextBoxSocialSecurity.Text, TextBoxPassword.Text);

                    Session["CurrentUser"] = currentApplicant;

                    Response.Redirect("~/Default.aspx");
                }
                else
                {
                    LabelSocialSecurityPassword.Visible = true;
                }

            }
            else if (TextBoxSocialSecurity.Text.StartsWith("Admin"))
            {
                AdminLoggedIn = AdminDB.AdminIsLoggedIn(TextBoxSocialSecurity.Text, TextBoxPassword.Text);
                if (AdminLoggedIn)
                {
                    var currentAdmin = AdminDB.GetAdmin(TextBoxSocialSecurity.Text, TextBoxPassword.Text);

                    Session["CurrentAdmin"] = currentAdmin;

                    if (currentAdmin.AdminTitle == CvModel.AdminTitle.SuperAdmin)
                    {
                        Response.Redirect("~/AdministerAdmin.aspx");
                    }
                    else if (currentAdmin.AdminTitle == CvModel.AdminTitle.Headhunter)
                    {
                        Response.Redirect("~/Headhunter.aspx");
                    }
                    else if (currentAdmin.AdminTitle == CvModel.AdminTitle.WorkAdmin)
                    {
                        Response.Redirect("~/WorkAdmin.aspx");
                    }
                    else
                    {
                        Response.Redirect("~/Default.aspx");
                    }
                }
                else
                {
                    LabelSocialSecurityPassword.Visible = true;
                }
            }
            else
            {
                LabelSocialSecurityPassword.Visible = true;
            }

        }

        public bool IsSocialSecurityNumber(string isNumber)
        {
            long number;
            var result = Int64.TryParse(isNumber, out number);

            if (isNumber.Length == 10)
            {
                return result;
            }
            else
            {
                return false;
            }
        }

        protected void TextBoxSocialSecurity_TextChanged(object sender, EventArgs e)
        {

        }
    }
}